package com.example.demo3.captcha;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.captcha.*;

import java.io.IOException;

@RequestMapping("/captcha")
@RestController
public class CaptchaController {
    @RequestMapping("/getcaptcha")
    public void getCaptcha(HttpServletResponse response, HttpSession session) throws IOException {
        response.setHeader("Pragma", "no-cache");
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 50, 4, 4);
     String code=captcha.getCode();
       session.setAttribute("Key",code);
       captcha.write(response.getOutputStream());
    }
    @RequestMapping("/check")
    public boolean check(HttpSession session,String captcha) {
        if(!StringUtils.hasLength(captcha)){
            return false;
        }
        String code=(String)session.getAttribute("Key");
        if(code.equalsIgnoreCase(captcha)){
            return true;
        }
        return false;
    }
}
